<?php
class articleControl extends fnControl{
	//显示文章
	public function index(){
		$db = K('news');
		$count = $db->where('recycled=0')->count();
		$page = new page($count,2);
		$pageList = $page->show(4);
		$arList =  $db->where('recycled=0')->field('cname,username,id,title,click')->limit($page->limit())->all();		
		$this->assign('arList', $arList);	
		$this->assign('pageList', $pageList);
		$this->display();
	}
	//显示添加文章的页面
	public function add_show(){	
		$source = $this->get_source();
		$result = $this->get_category();
		$this->assign('source', $source);
		$this->assign('category', $result);
		$this->display();
	}
	//处理文章添加方法
	public function add(){

		$this->verify();
		$db = R ( 'news' );
		$db->join = array (
			'news_data' => array (    // 关联表
			'type' => 'has_one',      // 包含一条主表记录
			'foreign_key' => 'nid'    // 关联字段
			)		 
		);
		$_POST['addtime']=time();
		$_POST['updatetime']=time();
		$_POST['thumb']=$_POST['thumb'][1][0];
		if($db->insert()){
			$this->success("发表成功",'index');
		}else{
			$this->error($this->error,'add_show');
		}
	}
	//编辑页面显示
	public function edit_show(){
		$id = isset($_GET['id'])?$_GET['id']:$this->error('文章id为空');
		$db = K('news');
		$data = $db->field('news.uid,source,cid,cname,username,news_data.body,title,click,news.id,thumb,news.keywords,description')->where('id='.$id)->find();
		$result = $this->get_category();
		$source = $this->get_source();
		$this->assign('sourceaaa', $source);
		$this->assign('category', $result);
		$this->assign('data', $data);
		$this->display();
	}
	//处理编辑文章的方法
	public function edit(){
		$db = K('news');
		$_POST['updatetime'] = time();
		if($db->save()){
			$this->success('更新成功','index');
		}else{
			$this->error($this->error,'index');
		}		
	}
	//处理删除文章的方法
	public function del(){
		$db = M('news');
		$id = isset($_GET['id'])?$_GET['id']:$this->error('文章id为空');		
		if($db->where('id='.$id)->save(array('recycled'=>'1'))){
			$this->success('已放入回收站','index');
		}else{
			$this->error($this->error,'index');
		}		
	}
	//全选删除
	public function ajax_del(){
		C("DEBUG",0);
		if(isset($_POST['id'])){
			$id = $_POST['id'];
		}else{
			echo 0;
			exit;
		}		
		$db = M('news');
		if($db->in($id)->save(array('recycled'=>'1'))){
			echo 1;
		}else{
			echo 0;
		}		
	}
	//显示回收站
	public function recycle(){
		$db = K('news');
		$count = $db->where('recycled=1')->count();
		$page = new page($count,2);
		$pageList = $page->show(4);
		$arList =  $db->where('recycled=1')->field('cname,username,id,title,click')->limit($page->limit())->all();
		$this->assign('arList', $arList);
		$this->assign('pageList', $pageList);
		$this->display();		
	}
	//获得栏目
	public function get_category(){
		$db = M('category');
		$data = $db->field('cname,catid,pid')->all();
		$result = arr::channel($data,'catid','pid',0,0,2,'>');
		return $result;
	}
	//获取来源
	public function get_source(){
		$db = M('source');
		$source = $db->field('source.sid,sname')->all();
		return $source;
	}
	//彻底删除
	public function thorough_del(){
		$db = K('news');
		$id = isset($_GET['id'])?$_GET['id']:$this->error('文章id为空');
		if($db->where('id='.$id)->delete()){
			$this->success('删除成功','recycle');
		}else{
			$this->error($this->error,'recycle');
		}		
	}
	//还原文章
	public function restore(){
		$db = M('news');
		$id = isset($_GET['id'])?$_GET['id']:$this->error('文章id为空');
		if($db->where('id='.$id)->save(array('recycled'=>'0'))){
			$this->success('还原成功','recycle');
		}else{
			$this->error($this->error,'recycle');
		}		
	}
	//ajax添加来源
	public function add_source(){
		$this->verify();		
		$db = M('source');	
		if($db->insert()){
			echo "1";
		}else{
			echo "0";
		}
		
	}
	
	
}














?>